﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace BattleCity
{
    public class MainMenu
    {
        Vector2[] stringsPositions;
        Vector2 menuWrapperPosition;

        SpriteFont menuFont;
        Texture2D menuWrapper;
        Texture2D button;
        Texture2D tapped;
        SpriteBatch spriteBatch;

        int tappedButton = 0;
        float tappedCounter = 0;

        MenuStates selected = MenuStates.None;

        int buttonX;

        public MainMenu(Vector2 screenCenter, SpriteFont menuFont, Texture2D menuWrapper, Texture2D button, Texture2D tapped, SpriteBatch spriteBatch)
        {
            stringsPositions = new Vector2[2];

            this.menuWrapper = menuWrapper;
            this.button = button;
            this.tapped = tapped;
            this.menuFont = menuFont;
            this.spriteBatch = spriteBatch;
            

            stringsPositions = new Vector2[4];
            stringsPositions[0] = new Vector2(screenCenter.X - menuFont.MeasureString(strings.SURVIVAL).X * 0.5f, 50.0f);
            stringsPositions[1] = new Vector2(screenCenter.X - menuFont.MeasureString(strings.SINGLELEVEL).X * 0.5f, 160.0f);
            stringsPositions[2] = new Vector2(screenCenter.X - menuFont.MeasureString(strings.SCORES).X * 0.5f, 270.0f);
            stringsPositions[3] = new Vector2(screenCenter.X - menuFont.MeasureString(strings.OPTIONS).X * 0.5f, 380.0f);

            buttonX = (int)screenCenter.X - (button.Width / 2);
            #if WINDOWS_PHONE 
            menuWrapperPosition = new Vector2(screenCenter.X - 200, 0.0f);
            #else
            menuWrapperPosition = new Vector2(screenCenter.X - 300, 0.0f);
            
#endif
        }

        public void Update(Vector2 gsPosition)
        {
            if (tappedCounter == 0.0f)
            {
                if ((gsPosition.X >= menuWrapperPosition.X) && (gsPosition.X <= menuWrapperPosition.X + menuWrapper.Width))
                {
                    if (gsPosition.Y < stringsPositions[0].Y + (menuFont.MeasureString(strings.SURVIVAL).Y * 1.5f))
                    {
                        selected = MenuStates.Survival;
                    }
                    else if (gsPosition.Y < stringsPositions[1].Y + (menuFont.MeasureString(strings.SINGLELEVEL).Y * 1.5f))
                    {
                        selected = MenuStates.SingleLevel;
                    }
                    else if (gsPosition.Y < stringsPositions[2].Y + (menuFont.MeasureString(strings.SCORES).Y * 1.5f))
                    {
                        selected = MenuStates.Scores;
                    }
                    else if (gsPosition.Y < stringsPositions[3].Y + (menuFont.MeasureString(strings.OPTIONS).Y * 1.5f))
                    {
                        selected = MenuStates.Options;
                    }

                    if (selected != MenuStates.None)
                    {
                        if (Options.soundEnabled) Sound.playSound(ref Sound.buttonPressed);
                        tappedCounter = 0.2f;
                    }
                }
            }
        }

        public void UpdateCounter(ref MenuStates menuState, float dt)
        {
            if (tappedCounter > 0.0f)
            {
                tappedCounter -= dt;
                
            }

            if((tappedCounter <= 0.0f) && (selected != MenuStates.None))
            {
                tappedCounter = 0.0f;
                menuState = selected;
                selected = MenuStates.None;
            }
        }

        public void Draw()
        {
            spriteBatch.Draw(menuWrapper, menuWrapperPosition, Color.White);

            spriteBatch.Draw(getButton(MenuStates.Survival), new Vector2(buttonX, stringsPositions[0].Y - 20), Color.White);
            spriteBatch.Draw(getButton(MenuStates.SingleLevel), new Vector2(buttonX, stringsPositions[1].Y - 20), Color.White);
            spriteBatch.Draw(getButton(MenuStates.Scores), new Vector2(buttonX, stringsPositions[2].Y - 20), Color.White);
            spriteBatch.Draw(getButton(MenuStates.Options), new Vector2(buttonX, stringsPositions[3].Y - 20), Color.White);

            spriteBatch.DrawString(menuFont, strings.SURVIVAL, stringsPositions[0] + new Vector2(2,2), Color.Black);
            spriteBatch.DrawString(menuFont, strings.SINGLELEVEL, stringsPositions[1] + new Vector2(2, 2), Color.Black);
            spriteBatch.DrawString(menuFont, strings.SCORES, stringsPositions[2] + new Vector2(2, 2), Color.Black);
            spriteBatch.DrawString(menuFont, strings.OPTIONS, stringsPositions[3] + new Vector2(2, 2), Color.Black);
           
            spriteBatch.DrawString(menuFont, strings.SURVIVAL, stringsPositions[0], Color.DarkCyan);
            spriteBatch.DrawString(menuFont, strings.SINGLELEVEL, stringsPositions[1], Color.DarkCyan);
            spriteBatch.DrawString(menuFont, strings.SCORES, stringsPositions[2], Color.DarkCyan);
            spriteBatch.DrawString(menuFont, strings.OPTIONS, stringsPositions[3], Color.DarkCyan);

            
        }

        Texture2D getButton(MenuStates buttonPressed)
        {
            if (selected == buttonPressed) return tapped;
            else return button;
        }
    }
}
